﻿/*
 * @author: wizardc
 */

using Dou.UI;
using DouEditor.Utils;
using TMPro;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

namespace DouEditor.UI
{
    [CustomEditor(typeof(DScaleButton), true)]
    [CanEditMultipleObjects]
    public class DScaleButtonEditor : DButtonEditor
    {
        [MenuItem("GameObject/DouUI/ScaleButton", false, 10)]
        private static void CreateUI(MenuCommand menuCommand)
        {
            var go = UIEditorUtil.CreateUIElement("Button");
            var image = go.AddComponent<DImage>();
            image.color = Color.white;
            image.type = Image.Type.Sliced;
            var button = go.AddComponent<DScaleButton>();
            button.targetGraphic = image;
            GameObject txtObj = UIEditorUtil.CreateGameObject("Text", go.transform);
            var text = txtObj.AddComponent<DText>();
            text.text = "Button";
            text.color = Color.black;
            text.alignment = TextAnchor.MiddleCenter;
            text.supportRichText = false;
            UIEditorUtil.PlaceUIElementRoot(go, menuCommand);
        }
        
        [MenuItem("GameObject/DouUI/ScaleButton - TextMeshPro", false, 10)]
        private static void CreateUI_TMP(MenuCommand menuCommand)
        {
            var go = UIEditorUtil.CreateUIElement("Button (TMP)");
            var image = go.AddComponent<DImage>();
            image.color = Color.white;
            image.type = Image.Type.Sliced;
            var button = go.AddComponent<DScaleButton>();
            button.targetGraphic = image;
            GameObject txtObj = UIEditorUtil.CreateGameObject("Text", go.transform);
            var text = txtObj.AddComponent<DTextMeshProUGUI>();
            text.text = "Button";
            text.color = Color.black;
            text.alignment = TextAlignmentOptions.Center;
            text.richText = false;
            UIEditorUtil.PlaceUIElementRoot(go, menuCommand);
        }
        
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            serializedObject.Update();
            EditorUtil.DrawNonPublicField(target, "_downScale");
            EditorGUILayout.PropertyField(serializedObject.FindProperty("_targetTransform"), true);
            serializedObject.ApplyModifiedProperties();
        }
    }
}
